package tomsung.cloud.agdc.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import tomsung.cloud.agdc.bean.Module;
import tomsung.cloud.agdc.bean.User;
import tomsung.cloud.agdc.security.SecurityUtil;
import tomsung.cloud.agdc.service.ModuleService;

@Controller
public class FrameController {
	
	@Autowired
	private ModuleService moduleService;
	
	@RequestMapping(value = "/frame", method = RequestMethod.GET)
	protected ModelAndView main(final HttpServletRequest request, final ModelAndView mav) {
		setModelAndViewMenu(request,mav);
		mav.setViewName("/frame/index");
		return mav;  
	}
	
	@RequestMapping(value = "/manualvalve", method = RequestMethod.GET)
	protected ModelAndView manualvalve(final HttpServletRequest request, final ModelAndView mav) {
		setModelAndViewMenu(request,mav);
		mav.setViewName("/manualvalve/index");
		return mav;
	}
	
	@RequestMapping(value = "/manualfan", method = RequestMethod.GET)
	protected ModelAndView manualfan(final HttpServletRequest request, final ModelAndView mav) {
		setModelAndViewMenu(request,mav);
		mav.setViewName("/manualfan/index");
		return mav;
	}
	
	@RequestMapping(value = "/manualroll", method = RequestMethod.GET)
	protected ModelAndView manualroll(final HttpServletRequest request, final ModelAndView mav) {
		setModelAndViewMenu(request,mav);
		mav.setViewName("/manualroll/index");
		return mav;
	}
	
	@RequestMapping(value = "/cycleplan", method = RequestMethod.GET)
	protected ModelAndView cycleplan(final HttpServletRequest request, final ModelAndView mav) {
		setModelAndViewMenu(request,mav);
		mav.setViewName("/cycleplan/index");
		return mav;
	}
	
	// 
	@RequestMapping(value = "/triggerplan", method = RequestMethod.GET)
	protected ModelAndView triggerplan(final HttpServletRequest request, final ModelAndView mav) {
		setModelAndViewMenu(request,mav);
		mav.setViewName("/triggerplan/index");
		return mav;
	}
	
	@RequestMapping(value = "/fanplan", method = RequestMethod.GET)
	protected ModelAndView fanplan(final HttpServletRequest request, final ModelAndView mav) {
		setModelAndViewMenu(request,mav);
		mav.setViewName("/fanplan/index");
		return mav;
	}
	
	@RequestMapping(value = "/monitor", method = RequestMethod.GET)
	protected ModelAndView monitor(final HttpServletRequest request, final ModelAndView mav) {
		setModelAndViewMenu(request,mav);
		mav.setViewName("/monitor/index");
		return mav;
	}
	
	@RequestMapping(value = "/operlog", method = RequestMethod.GET)
	protected ModelAndView operlog(final HttpServletRequest request, final ModelAndView mav) {
		setModelAndViewMenu(request,mav);
		mav.setViewName("/operlog/index");
		return mav;
	}
	
	@RequestMapping(value = "/user", method = RequestMethod.GET)
	protected ModelAndView user(final HttpServletRequest request, final ModelAndView mav) {
		setModelAndViewMenu(request,mav);
		mav.setViewName("/user/index");
		return mav;
	}
	
	@RequestMapping(value = "/environmentdata", method = RequestMethod.GET)
	protected ModelAndView environmentdata(final HttpServletRequest request, final ModelAndView mav) {
		setModelAndViewMenu(request,mav);
		mav.setViewName("/environmentdata/index");
		return mav;
	}
	

	@RequestMapping(value = "/expertsystem", method = RequestMethod.GET)
	protected ModelAndView expertsystem(final HttpServletRequest request, final ModelAndView mav) {
		setModelAndViewMenu(request,mav);
		mav.setViewName("/expertsystem/index");
		return mav;
	}
	
	@RequestMapping(value = "/onlinestore", method = RequestMethod.GET)
	protected ModelAndView onlinestore(final HttpServletRequest request, final ModelAndView mav) {
		setModelAndViewMenu(request,mav);
		mav.setViewName("/onlinestore/index");
		return mav;
	}
	
	@RequestMapping(value = "/plantexp", method = RequestMethod.GET)
	protected ModelAndView plantexp(final HttpServletRequest request, final ModelAndView mav) {
		setModelAndViewMenu(request,mav);
		mav.setViewName("/plantexp/index");
		return mav;
	}
	
	@RequestMapping(value = "/managesheds", method = RequestMethod.GET)
    protected ModelAndView managesheds(final HttpServletRequest request, final ModelAndView mav) {
        setModelAndViewMenu(request,mav);
        mav.setViewName("/managesheds/index");
        return mav;
    }
	
	@RequestMapping(value = "/managecontrollers", method = RequestMethod.GET)
    protected ModelAndView managecontroller(final HttpServletRequest request, final ModelAndView mav) {
        setModelAndViewMenu(request,mav);
        mav.setViewName("/managecontrollers/index");
        return mav;
    }
	
	@RequestMapping(value = "/managedevices", method = RequestMethod.GET)
	protected ModelAndView managedevices(final HttpServletRequest request, final ModelAndView mav) {
		setModelAndViewMenu(request,mav);
		mav.setViewName("/managedevices/index");
		return mav;
	}

	@RequestMapping(value = "/shedenvironmentdata", method = RequestMethod.GET)
	protected ModelAndView shedenvironmentdata(final HttpServletRequest request, final ModelAndView mav) {
		setModelAndViewMenu(request,mav);
		mav.setViewName("/shedenvironmentdata/index");
		return mav;
	}
	
	@RequestMapping(value = "/shedyield", method = RequestMethod.GET)
	protected ModelAndView shedyield(final HttpServletRequest request, final ModelAndView mav) {
		setModelAndViewMenu(request,mav);
		mav.setViewName("/shedyield/index");
		return mav;
	}
	
	private void setModelAndViewMenu(final HttpServletRequest request, final ModelAndView mav) {
		User user = SecurityUtil.getLoginUser(request);
		List<Module> modules = moduleService.getModules(user);
		mav.addObject("modules", modules);
	}
}
